package com.czwj.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.DailyReport;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PlanDaily;
import com.czwj.entities.UserInfo;
import com.czwj.service.DailyReportService;
import com.czwj.service.PlanDailyService;

@Controller
public class ReportAction {
	
	@Resource
	DailyReportService dailyReportService;
	@Resource
	PlanDailyService planDailyService;
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	@RequestMapping(value = "/existplanandreportdate", method = RequestMethod.GET)
	@ResponseBody
	public Map existreportdate(@RequestParam(value = "projectitemid") int projectitemid,
			@RequestParam(value = "yearmonth") String yearmonth) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("projectitemid", projectitemid);
		map.put("yearmonth", yearmonth);
		List<String> list = dailyReportService.existreportdate(map);
		List<String> planlist = planDailyService.existplandate(map);
		Map<String, Object> result = new HashMap<>();
		result.put("report", list);
		result.put("plan", planlist);
		return result;
	}
	
	@SystemControllerLog(description = "修改工作汇报")
	@RequestMapping(value = "/dailyreportSave.do", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult dailyreportSave(@RequestBody List<DailyReport> list,
			@RequestParam(value = "projectitemid") int projectitemid, HttpServletRequest request)throws Exception {
		OperateResult result = new OperateResult();
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		int count = dailyReportService.editdailyreport(list,userInfo.getId(),request.getRemoteAddr(),projectitemid);
		
		result.setSuccess(count > 0);
		return result;
	}
	
	
	@RequestMapping(value = "/dailyreports", method = RequestMethod.GET)
	@ResponseBody
	public List<DailyReport> dailyreports(@RequestParam("projectitemid") int projectitemid,
			@RequestParam("plandate") Date plandate){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("projectitemid", projectitemid);
		map.put("plandate", sdf.format(plandate));
		List<DailyReport> list = dailyReportService.search(map);
		return list;
	}
}
